<!DOCTYPE html>
<html lang=zh>
<head>
  <meta charset="utf-8">
  <meta name=referrer content=never>
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-transform" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="format-detection" content="telephone=no,email=no,adress=no">
  <!-- Color theme for statusbar -->
  <meta name="theme-color" content="#000000" />
  <!-- 强制页面在当前窗口以独立页面显示,防止别人在框架里调用页面 -->
  <meta http-equiv="window-target" content="_top" />
  
  
  <title>Spring aop 对象内部方法间的嵌套失效 | JiSong‘s Blog</title>
  <meta name="description" content="转自：Spring aop 对象内部方法间的嵌套失效  先看一下spring 代理原理：  spring代理嵌套方法调用不生效  Spring AOP defaults to using standard JDK dynamic proxies for AOP proxies. This enables any interface (or set of interfaces) to be pro">
<meta name="keywords" content="Spring,AOP">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring aop 对象内部方法间的嵌套失效">
<meta property="og:url" content="https://ji-song.gitee.io/blog/2018/07/11/Spring-aop-对象内部方法间的嵌套失效/index.html">
<meta property="og:site_name" content="JiSong">
<meta property="og:description" content="转自：Spring aop 对象内部方法间的嵌套失效  先看一下spring 代理原理：  spring代理嵌套方法调用不生效  Spring AOP defaults to using standard JDK dynamic proxies for AOP proxies. This enables any interface (or set of interfaces) to be pro">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2018-07-11T15:02:11.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Spring aop 对象内部方法间的嵌套失效">
<meta name="twitter:description" content="转自：Spring aop 对象内部方法间的嵌套失效  先看一下spring 代理原理：  spring代理嵌套方法调用不生效  Spring AOP defaults to using standard JDK dynamic proxies for AOP proxies. This enables any interface (or set of interfaces) to be pro">
  <!-- Canonical links -->
  <link rel="canonical" href="https://ji-song.gitee.io/blog/2018/07/11/Spring-aop-对象内部方法间的嵌套失效/index.html">
  
    <link rel="alternate" href="/atom.xml" title="JiSong" type="application/atom+xml">
  
  
    <link rel="icon" href="/blog/favicon.png" type="image/x-icon">
  
  <!-- font-awesome CSS -->
  <!-- <link href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"> -->
  <link rel="stylesheet" href="/blog/css/style.css">
  
    
    

</head>


<body class="main-center theme-green" itemscope itemtype="http://schema.org/WebPage">
  <header class="header" itemscope itemtype="http://schema.org/WPHeader">
  <div class="slimContent">
    <div class="navbar-header">
      
      
      <div class="profile-block text-center">
        <a id="avatar" href="https://gitee.com/ji-song" target="_blank">
          <img class="img-circle img-rotate" src="/blog/images/avatar.jpeg" width="200" height="200">
        </a>
        <h2 id="name" class="hidden-xs hidden-sm">JiSong</h2>
        <h3 id="title" class="hidden-xs hidden-sm hidden-md">Java Coder</h3>
        <small id="location" class="text-muted hidden-xs hidden-sm"><i class="icon icon-map-marker"></i> HangZhou, China</small>
      </div>
      
      <div class="search" id="search-form-wrap">

    <form class="search-form sidebar-form">
        <div class="input-group">
            <input type="text" class="search-form-input form-control" placeholder="搜索" />
            <span class="input-group-btn">
                <button type="submit" class="search-form-submit btn btn-flat" onclick="return false;"><i class="icon icon-search"></i></button>
            </span>
        </div>
    </form>
    <div class="ins-search">
  <div class="ins-search-mask"></div>
  <div class="ins-search-container">
    <div class="ins-input-wrapper">
      <input type="text" class="ins-search-input" placeholder="想要查找什么..." x-webkit-speech />
      <button type="button" class="close ins-close ins-selectable" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
    </div>
    <div class="ins-section-wrapper">
      <div class="ins-section-container"></div>
    </div>
  </div>
</div>


</div>
      <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#main-navbar" aria-controls="main-navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <nav id="main-navbar" class="collapse navbar-collapse" itemscope itemtype="http://schema.org/SiteNavigationElement" role="navigation">
      <ul class="nav navbar-nav main-nav">
        
        
        
        <li class="menu-item menu-item-home">
          <a href="/blog/.">
            
            <i class="icon icon-home-fill"></i>
            
            <span class="menu-title">首页</span>
          </a>
        </li>
        
        
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives">
            
            <i class="icon icon-archives-fill"></i>
            
            <span class="menu-title">所有文章</span>
          </a>
        </li>
        
        
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories">
            
            <i class="icon icon-folder"></i>
            
            <span class="menu-title">分类</span>
          </a>
        </li>
        
        
        
        <li class="menu-item menu-item-about">
          <a href="/blog/about">
            
            <i class="icon icon-cup-fill"></i>
            
            <span class="menu-title">关于</span>
          </a>
        </li>
        
      </ul>
      
	
    <ul class="social-links">
    	
        <li><a href="https://gitee.com/ji-song" target="_blank" title="Github" data-toggle=tooltip data-placement=top><i class="icon icon-github"></i></a></li>
        
    </ul>

    </nav>
  </div>
</header>

  
    <aside class="sidebar" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    
      <div class="widget">
    <h3 class="widget-title">了解一下</h3>
    <div class="widget-body">
        <div id="board">
            <div class="content">
                <p>欢迎交流与分享经验!来来来</p>
            </div>
        </div>
    </div>
</div>

    
      
  <div class="widget">
    <h3 class="widget-title">分类</h3>
    <div class="widget-body">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/blog/categories/技能/">技能</a><span class="category-list-count">19</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/生活/">生活</a><span class="category-list-count">2</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">标签</h3>
    <div class="widget-body">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/AOP/">AOP</a><span class="tag-list-count">7</span></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/DB/">DB</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/JDK/">JDK</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/JVM/">JVM</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/Kotlin/">Kotlin</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/Log/">Log</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/MVC/">MVC</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/Spring/">Spring</a><span class="tag-list-count">10</span></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/job/">job</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/kotlin/">kotlin</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/linux/">linux</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/plan/">plan</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/sublime/">sublime</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/事务/">事务</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/坑记/">坑记</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/并发/">并发</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/集合/">集合</a><span class="tag-list-count">2</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">标签云</h3>
    <div class="widget-body tagcloud">
      <a href="/blog/tags/AOP/" style="font-size: 13.75px;">AOP</a> <a href="/blog/tags/DB/" style="font-size: 13px;">DB</a> <a href="/blog/tags/JDK/" style="font-size: 13.5px;">JDK</a> <a href="/blog/tags/JVM/" style="font-size: 13.5px;">JVM</a> <a href="/blog/tags/Kotlin/" style="font-size: 13px;">Kotlin</a> <a href="/blog/tags/Log/" style="font-size: 13px;">Log</a> <a href="/blog/tags/MVC/" style="font-size: 13px;">MVC</a> <a href="/blog/tags/Spring/" style="font-size: 14px;">Spring</a> <a href="/blog/tags/job/" style="font-size: 13px;">job</a> <a href="/blog/tags/kotlin/" style="font-size: 13px;">kotlin</a> <a href="/blog/tags/linux/" style="font-size: 13px;">linux</a> <a href="/blog/tags/plan/" style="font-size: 13px;">plan</a> <a href="/blog/tags/sublime/" style="font-size: 13px;">sublime</a> <a href="/blog/tags/事务/" style="font-size: 13px;">事务</a> <a href="/blog/tags/坑记/" style="font-size: 13px;">坑记</a> <a href="/blog/tags/并发/" style="font-size: 13px;">并发</a> <a href="/blog/tags/集合/" style="font-size: 13.25px;">集合</a>
    </div>
  </div>

    
      
  <div class="widget">
    <h3 class="widget-title">所有文章</h3>
    <div class="widget-body">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2018/08/">八月 2018</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2018/07/">七月 2018</a><span class="archive-list-count">12</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2018/06/">六月 2018</a><span class="archive-list-count">7</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget-body">
      <ul class="recent-post-list list-unstyled no-thumbnail">
        
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/blog/categories/技能/">技能</a>
              </p>
              <p class="item-title">
                <a href="/blog/2018/08/08/JAVA对象的两种访问定位方式/" class="title">JAVA对象的两种访问定位方式</a>
              </p>
              <p class="item-date">
                <time datetime="2018-08-08T06:41:32.000Z" itemprop="datePublished">2018年08月08日</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/blog/categories/技能/">技能</a>
              </p>
              <p class="item-title">
                <a href="/blog/2018/08/07/spring-mvc源码阅读-DispatcherServlet/" class="title">spring mvc源码阅读 - DispatcherServlet</a>
              </p>
              <p class="item-date">
                <time datetime="2018-08-07T01:57:38.000Z" itemprop="datePublished">2018年08月07日</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/blog/categories/技能/">技能</a>
              </p>
              <p class="item-title">
                <a href="/blog/2018/07/26/ACID/" class="title">ACID</a>
              </p>
              <p class="item-date">
                <time datetime="2018-07-26T14:24:03.000Z" itemprop="datePublished">2018年07月26日</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/blog/categories/技能/">技能</a>
              </p>
              <p class="item-title">
                <a href="/blog/2018/07/12/Kotlin-的异常/" class="title">Kotlin 的异常</a>
              </p>
              <p class="item-date">
                <time datetime="2018-07-12T13:42:03.000Z" itemprop="datePublished">2018年07月12日</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/blog/categories/技能/">技能</a>
              </p>
              <p class="item-title">
                <a href="/blog/2018/07/12/Spring-AOP-玩一下异常的流程/" class="title">Spring AOP 玩一下异常的流程</a>
              </p>
              <p class="item-date">
                <time datetime="2018-07-12T13:39:07.000Z" itemprop="datePublished">2018年07月12日</time>
              </p>
            </div>
          </li>
          
      </ul>
    </div>
  </div>
  

    
  </div>
</aside>

  
  
<main class="main" role="main">
  <div class="content">
  <article id="post-Spring-aop-对象内部方法间的嵌套失效" class="article article-type-post" itemscope itemtype="http://schema.org/BlogPosting">
    
    <div class="article-header">
      
        
  
    <h1 class="article-title" itemprop="name">
      Spring aop 对象内部方法间的嵌套失效
    </h1>
  

      
      <div class="article-meta">
        <span class="article-date">
    <i class="icon icon-calendar-check"></i>
	<a href="/blog/2018/07/11/Spring-aop-对象内部方法间的嵌套失效/" class="article-date">
	  <time datetime="2018-07-11T13:57:51.000Z" itemprop="datePublished">2018年07月11日</time>
	</a>
</span>
        
  <span class="article-category">
    <i class="icon icon-folder"></i>
    <a class="article-category-link" href="/blog/categories/技能/">技能</a>
  </span>

        
  <span class="article-tag">
    <i class="icon icon-tags"></i>
	<a class="article-tag-link" href="/blog/tags/AOP/">AOP</a>, <a class="article-tag-link" href="/blog/tags/Spring/">Spring</a>
  </span>


        

        <span class="post-comment"><i class="icon icon-comment"></i> <a href="/blog/2018/07/11/Spring-aop-对象内部方法间的嵌套失效/#comments" class="article-comment-link">评论</a></span>
        
      </div>
    </div>
    <div class="article-entry markdown-body" itemprop="articleBody">
      
        <blockquote>
<p>转自：<a href="https://blog.csdn.net/doctor_who2004/article/details/51814476" target="_blank" rel="noopener">Spring aop 对象内部方法间的嵌套失效</a></p>
</blockquote>
<p>先看一下spring 代理原理：</p>
<ul>
<li>spring代理嵌套方法调用不生效<ul>
<li></li>
<li>Spring AOP defaults to using standard JDK dynamic proxies for AOP</li>
<li>proxies. This enables any interface (or set of interfaces) to be</li>
<li>proxied.</li>
<li></li>
<li>Spring AOP can also use CGLIB proxies. This is necessary to proxy</li>
<li>classes rather than interfaces. CGLIB is used by default if a business</li>
<li>object does not implement an interface. As it is good practice to</li>
<li>program to interfaces rather than classes; business classes normally</li>
<li>will implement one or more business interfaces. It is possible to force</li>
<li>the use of CGLIB, in those (hopefully rare) cases where you need to</li>
<li>advise a method that is not declared on an interface, or where you need</li>
<li>to pass a proxied object to a method as a concrete type.</li>
<li></li>
<li>It is important to grasp the fact that Spring AOP is proxy-based. See</li>
<li>Section 11.6.1, “Understanding AOP proxies” for a thorough examination</li>
<li>of exactly what this implementation detail actually means.</li>
<li></li>
<li></li>
<li></li>
<li>通过配置 &lt;aop:aspectj-autoproxy proxy-target-class=”true” /&gt;可以统一用CGLIB</li>
<li>proxies。</li>
<li></li>
</ul>
</li>
</ul>
<p>实际上注入到spring容器的类实例是代理类，自然我们调用类的方法都能被aop拦截，但是类中内部方法的调用即this.metho(…), 这里的this，并不是spirng生成的代理类实例，而是实际产生的原对象，自然，aop就不会拦截这样的方法调用。</p>
<p>写个简短的程序验证一下spring代理嵌套方法调用不生效。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">package com.doctor.aop.demo;</span><br><span class="line"> </span><br><span class="line">import org.springframework.stereotype.Component;</span><br><span class="line"> </span><br><span class="line">/**</span><br><span class="line"> * @description </span><br><span class="line"> *</span><br><span class="line"> * @author sdcuike</span><br><span class="line"> *</span><br><span class="line"> * @date 2016年6月24日 下午6:44:32</span><br><span class="line"> */</span><br><span class="line">@Component</span><br><span class="line">public class Demo &#123;</span><br><span class="line">	public void test01() &#123;</span><br><span class="line">		System.out.println(&quot;test1&quot;);</span><br><span class="line">		System.out.println(&quot;this对象指的不是代理类对象，而是原对象&quot; + this.getClass());</span><br><span class="line">		test02(&quot;hello nest &quot;);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	public String test02(String name) &#123;</span><br><span class="line">		return name;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line">package com.doctor.aop.demo;</span><br><span class="line"> </span><br><span class="line">import org.springframework.context.support.ClassPathXmlApplicationContext;</span><br><span class="line"> </span><br><span class="line">/**</span><br><span class="line"> * @description</span><br><span class="line"> *</span><br><span class="line"> * @author sdcuike</span><br><span class="line"> *</span><br><span class="line"> * @date 2016年6月24日 下午6:44:12</span><br><span class="line"> * </span><br><span class="line"> *       spring代理嵌套方法调用不生效</span><br><span class="line"> * </span><br><span class="line"> *       Spring AOP defaults to using standard JDK dynamic proxies for AOP</span><br><span class="line"> *       proxies. This enables any interface (or set of interfaces) to be</span><br><span class="line"> *       proxied.</span><br><span class="line"> * </span><br><span class="line"> *       Spring AOP can also use CGLIB proxies. This is necessary to proxy</span><br><span class="line"> *       classes rather than interfaces. CGLIB is used by default if a business</span><br><span class="line"> *       object does not implement an interface. As it is good practice to</span><br><span class="line"> *       program to interfaces rather than classes; business classes normally</span><br><span class="line"> *       will implement one or more business interfaces. It is possible to force</span><br><span class="line"> *       the use of CGLIB, in those (hopefully rare) cases where you need to</span><br><span class="line"> *       advise a method that is not declared on an interface, or where you need</span><br><span class="line"> *       to pass a proxied object to a method as a concrete type.</span><br><span class="line"> * </span><br><span class="line"> *       It is important to grasp the fact that Spring AOP is proxy-based. See</span><br><span class="line"> *       Section 11.6.1, “Understanding AOP proxies” for a thorough examination</span><br><span class="line"> *       of exactly what this implementation detail actually means.</span><br><span class="line"> * </span><br><span class="line"> * </span><br><span class="line"> * </span><br><span class="line"> *       通过配置 &lt;aop:aspectj-autoproxy proxy-target-class=&quot;true&quot; /&gt;可以统一用CGLIB</span><br><span class="line"> *       proxies。</span><br><span class="line"> * </span><br><span class="line"> */</span><br><span class="line">public class MethodInvokeTimeAspectTest &#123;</span><br><span class="line"> </span><br><span class="line">	public static void main(String[] args) &#123;</span><br><span class="line">		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(</span><br><span class="line">				&quot;classpath:/aopDemo1/spring-aop.xml&quot;);</span><br><span class="line">		Demo demo = applicationContext.getBean(Demo.class);</span><br><span class="line">		demo.test01();</span><br><span class="line">		demo.test02(&quot;doctor who&quot;);</span><br><span class="line">		System.out.println(&quot;注入到spring容器的类实例是代理类&quot; + demo.getClass());</span><br><span class="line">		// class com.doctor.aop.demo.Demo$$EnhancerBySpringCGLIB$$e9a9050a</span><br><span class="line">		// 注入到spring容器的类实例是代理类</span><br><span class="line"> </span><br><span class="line">		InterfaceDemo interfaceDemo = applicationContext.getBean(InterfaceDemo.class);</span><br><span class="line">		System.out.println(&quot;注入到spring容器的类实例是代理类&quot; + interfaceDemo.getClass());</span><br><span class="line">		interfaceDemo.testAnother();</span><br><span class="line">		applicationContext.close();</span><br><span class="line">	&#125;</span><br><span class="line"> </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>看一下输出：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">07-03 11:23:57.357 main  INFO  org.springframework.context.support.ClassPathXmlApplicationContext -	Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@9f70c54: startup date [Sun Jul 03 11:23:57 CST 2016]; root of context hierarchy</span><br><span class="line">07-03 11:23:57.457 main  INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader -	Loading XML bean definitions from class path resource [aopDemo1/spring-aop.xml]</span><br><span class="line">AspectAopDemo===before  execution(void com.doctor.aop.demo.Demo.test01())</span><br><span class="line">test1</span><br><span class="line">this对象指的不是代理类对象，而是原对象class com.doctor.aop.demo.Demo</span><br><span class="line">AspectAopDemo===before  execution(String com.doctor.aop.demo.Demo.test02(String))</span><br><span class="line">AspectAopDemo===after   execution(String com.doctor.aop.demo.Demo.test02(String))</span><br><span class="line">AspectAopDemo===after   execution(void com.doctor.aop.demo.Demo.test01())</span><br><span class="line">07-03 11:23:59.108 main  INFO  com.doctor.aop.demo.MethodInvokeTimeAspect -	[execution(void com.doctor.aop.demo.Demo.test01())],[],[null], [20] </span><br><span class="line">AspectAopDemo===before  execution(String com.doctor.aop.demo.Demo.test02(String))</span><br><span class="line">AspectAopDemo===after   execution(String com.doctor.aop.demo.Demo.test02(String))</span><br><span class="line">07-03 11:23:59.108 main  INFO  com.doctor.aop.demo.MethodInvokeTimeAspect -	[execution(String com.doctor.aop.demo.Demo.test02(String))],[doctor who],[doctor who], [0] </span><br><span class="line">注入到spring容器的类实例是代理类class com.doctor.aop.demo.Demo$$EnhancerBySpringCGLIB$$595bc0d7</span><br><span class="line">注入到spring容器的类实例是代理类class com.sun.proxy.$Proxy7</span><br><span class="line">07-03 11:23:59.108 main  INFO  org.springframework.context.support.ClassPathXmlApplicationContext -	Closing org.springframework.context.support.ClassPathXmlApplicationContext@9f70c54: startup date [Sun Jul 03 11:23:57 CST 2016]; root of context hierarchy</span><br></pre></td></tr></table></figure></p>
<p>日志结果：注入到spring容器内的类实例是代理类实例，非原实例，而类内方法之间调用的时候，this指向的是原实例，非代理类。</p>
<p>由spring代理是动态的，运行时织入，固类内部方法之间调用的时候，不可能让this指向代理类。</p>
<p>源码地址：<a href="https://github.com/sdcuike/spring-demo/blob/master/src/main/java/com/doctor/aop/demo/MethodInvokeTimeAspectTest.java" target="_blank" rel="noopener">https://github.com/sdcuike/spring-demo/blob/master/src/main/java/com/doctor/aop/demo/MethodInvokeTimeAspectTest.java</a></p>
<h2 id="如何解决"><a href="#如何解决" class="headerlink" title="如何解决"></a>如何解决</h2><p> 动态代理不行，我们基于字节码的支持编译期间进行织入(weaving)，还是编译后(post-compile)的AspectJ。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">package com.doctor.aop.aspect.demo;</span><br><span class="line"> </span><br><span class="line">/**</span><br><span class="line"> * @description </span><br><span class="line"> *</span><br><span class="line"> * @author sdcuike</span><br><span class="line"> *</span><br><span class="line"> * @date 2016年6月27日 下午1:51:14</span><br><span class="line"> */</span><br><span class="line">public aspect AspectAopDemo &#123;</span><br><span class="line">pointcut demo() : execution(* com.doctor.aop.demo.Demo.* (..));</span><br><span class="line"> </span><br><span class="line">before() : demo() &#123;</span><br><span class="line">	System.out.println(&quot;AspectAopDemo===before  &quot; + thisJoinPoint);</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line">after() : demo() &#123;</span><br><span class="line">	System.out.println(&quot;AspectAopDemo===after   &quot; + thisJoinPoint);</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">package com.doctor.aop.demo;</span><br><span class="line"> </span><br><span class="line">import org.springframework.stereotype.Component;</span><br><span class="line"> </span><br><span class="line">/**</span><br><span class="line"> * @description </span><br><span class="line"> *</span><br><span class="line"> * @author sdcuike</span><br><span class="line"> *</span><br><span class="line"> * @date 2016年6月24日 下午6:44:32</span><br><span class="line"> */</span><br><span class="line">@Component</span><br><span class="line">public class Demo &#123;</span><br><span class="line">	public void test01() &#123;</span><br><span class="line">		System.out.println(&quot;test1&quot;);</span><br><span class="line">		System.out.println(&quot;this对象指的不是代理类对象，而是原对象&quot; + this.getClass());</span><br><span class="line">		test02(&quot;hello nest &quot;);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	public String test02(String name) &#123;</span><br><span class="line">		return name;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>看一下结果：eclipse IDE中把项目转变成AspectJ项目。</p>
<p>运行下面程序：（AspectJ/java application）<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">package com.doctor.aop.aspect.demo;</span><br><span class="line"> </span><br><span class="line">import org.springframework.context.support.ClassPathXmlApplicationContext;</span><br><span class="line"> </span><br><span class="line">import com.doctor.aop.demo.Demo;</span><br><span class="line"> </span><br><span class="line">/**</span><br><span class="line"> * @description</span><br><span class="line"> *</span><br><span class="line"> * @author sdcuike</span><br><span class="line"> *</span><br><span class="line"> * @date 2016年6月27日 下午2:16:57</span><br><span class="line"> * </span><br><span class="line"> *       aspect 编译时候注入，克服动态代理方法嵌套调用失效问题.</span><br><span class="line"> * </span><br><span class="line"> *       运行方法：把项目转换成AspectJ工程，运行的时候选择：Run As-&gt; AspectJ/Java Application</span><br><span class="line"> */</span><br><span class="line">class AspectAopDemoTest &#123;</span><br><span class="line"> </span><br><span class="line">	public static void main(String[] args) &#123;</span><br><span class="line">		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(</span><br><span class="line">				&quot;classpath:/aopDemo1/spring-aop2.xml&quot;);</span><br><span class="line">		Demo demo = applicationContext.getBean(Demo.class);</span><br><span class="line">		demo.test01();</span><br><span class="line">		demo.test02(&quot;doctor who&quot;);</span><br><span class="line">		System.out.println(&quot;注入到spring容器的类实例是代理类&quot; + demo.getClass());</span><br><span class="line">		// class com.doctor.aop.demo.Demo$$EnhancerBySpringCGLIB$$e9a9050a</span><br><span class="line">		// 注入到spring容器的类实例是代理类</span><br><span class="line"> </span><br><span class="line">		applicationContext.close();</span><br><span class="line">	&#125;</span><br><span class="line"> </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>输出结果：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">07-03 12:04:48.375 main  INFO  org.springframework.context.support.ClassPathXmlApplicationContext -	Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@13c78c0b: startup date [Sun Jul 03 12:04:48 CST 2016]; root of context hierarchy</span><br><span class="line">07-03 12:04:48.421 main  INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader -	Loading XML bean definitions from class path resource [aopDemo1/spring-aop2.xml]</span><br><span class="line">AspectAopDemo===before  execution(void com.doctor.aop.demo.Demo.test01())</span><br><span class="line">test1</span><br><span class="line">this对象指的不是代理类对象，而是原对象class com.doctor.aop.demo.Demo</span><br><span class="line">AspectAopDemo===before  execution(String com.doctor.aop.demo.Demo.test02(String))</span><br><span class="line">AspectAopDemo===after   execution(String com.doctor.aop.demo.Demo.test02(String))</span><br><span class="line">AspectAopDemo===after   execution(void com.doctor.aop.demo.Demo.test01())</span><br><span class="line">AspectAopDemo===before  execution(String com.doctor.aop.demo.Demo.test02(String))</span><br><span class="line">AspectAopDemo===after   execution(String com.doctor.aop.demo.Demo.test02(String))</span><br><span class="line">注入到spring容器的类实例是代理类class com.doctor.aop.demo.Demo</span><br><span class="line">07-03 12:04:48.794 main  INFO  org.springframework.context.support.ClassPathXmlApplicationContext -	Closing org.springframework.context.support.ClassPathXmlApplicationContext@13c78c0b: startup date [Sun Jul 03 12:04:48 CST 2016]; root of context hierarchy</span><br></pre></td></tr></table></figure></p>
<p>test02方法确实被调用了4次。</p>
<p>使用AspectJ的一个间接局限是，因为AspectJ通知可以应用于POJO之上，它有可能将通知应用于一个已配置的通知之上。对于一个你没有注意到这方面问题的大范围应用的通知，这有可能导致一个无限循环。</p>
<p>具体更详细的参考：<a href="http://www.oschina.net/translate/comparative_analysis_between_spring_aop_and_aspectj" target="_blank" rel="noopener">http://www.oschina.net/translate/comparative_analysis_between_spring_aop_and_aspectj</a></p>

      
    </div>
    <div class="article-footer">
      <blockquote class="mt-2x">
  <ul class="post-copyright list-unstyled">
    
    <li class="post-copyright-link hidden-xs">
      <strong>本文链接：</strong>
      <a href="https://ji-song.gitee.io/blog/2018/07/11/Spring-aop-对象内部方法间的嵌套失效/" title="Spring aop 对象内部方法间的嵌套失效" target="_blank" rel="external">https://ji-song.gitee.io/blog/2018/07/11/Spring-aop-对象内部方法间的嵌套失效/</a>
    </li>
    
    <li class="post-copyright-license">
      <strong>版权声明： </strong> 本博客所有文章除特别声明外，均采用 <a href="http://creativecommons.org/licenses/by/4.0/deed.zh" target="_blank" rel="external">CC BY 4.0 CN协议</a> 许可协议。转载请注明出处！
    </li>
  </ul>
</blockquote>


<div class="panel panel-default panel-badger">
  <div class="panel-body">
    <figure class="media">
      <div class="media-left">
        <a href="https://gitee.com/ji-song" target="_blank" class="img-burn thumb-sm visible-lg">
          <img src="/blog/images/avatar.jpeg" class="img-rounded w-full" alt="">
        </a>
      </div>
      <div class="media-body">
        <h3 class="media-heading"><a href="https://gitee.com/ji-song" target="_blank"><span class="text-dark">JiSong</span><small class="ml-1x">Java Coder</small></a></h3>
        <div>个人简介。</div>
      </div>
    </figure>
  </div>
</div>


    </div>
  </article>
  
    
  <section id="comments">
  	
  </section>


  
</div>

  <nav class="bar bar-footer clearfix" data-stick-bottom>
  <div class="bar-inner">
  
  <ul class="pager pull-left">
    
    <li class="prev">
      <a href="/blog/2018/07/11/Spring-AOP在缓存上的应用/" title="Spring AOP在缓存上的应用"><i class="icon icon-angle-left" aria-hidden="true"></i><span>&nbsp;&nbsp;上一篇</span></a>
    </li>
    
    
    <li class="next">
      <a href="/blog/2018/07/11/kotlin、SpringBoot配置AOP/" title="kotlin、SpringBoot配置AOP"><span>下一篇&nbsp;&nbsp;</span><i class="icon icon-angle-right" aria-hidden="true"></i></a>
    </li>
    
    
  </ul>
  
  
  <!-- Button trigger modal -->
  <button type="button" class="btn btn-fancy btn-donate pop-onhover bg-gradient-warning" data-toggle="modal" data-target="#donateModal"><span>赏</span></button>
  <!-- <div class="wave-icon wave-icon-danger btn-donate" data-toggle="modal" data-target="#donateModal">
    <div class="wave-circle"><span class="icon"><i class="icon icon-bill"></i></span></div>
  </div> -->
  
  
  <div class="bar-right">
    
    <div class="share-component" data-sites="weibo,qq,wechat,facebook,twitter" data-mobile-sites="weibo,qq,qzone"></div>
    
  </div>
  </div>
</nav>
  
<!-- Modal -->
<div class="modal modal-center modal-small modal-xs-full fade" id="donateModal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content donate">
      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      <div class="modal-body">
        <div class="donate-box">
          <div class="donate-head">
            <p>感谢您的支持，我会继续努力的!</p>
          </div>
          <div class="tab-content">
            <div role="tabpanel" class="tab-pane fade active in" id="alipay">
              <div class="donate-payimg">
                <img src="/blog/images/donate/alipayimg.jpg" alt="扫码支持" title="扫一扫" />
              </div>
              <p class="text-muted mv">扫码打赏，你说多少就多少</p>
              <p class="text-grey">打开支付宝扫一扫，即可进行扫码打赏哦</p>
            </div>
            <div role="tabpanel" class="tab-pane fade" id="wechatpay">
              <div class="donate-payimg">
                <img src="/blog/images/donate/wechatpayimg.jpg" alt="扫码支持" title="扫一扫" />
              </div>
              <p class="text-muted mv">扫码打赏，你说多少就多少</p>
              <p class="text-grey">打开微信扫一扫，即可进行扫码打赏哦</p>
            </div>
          </div>
          <div class="donate-footer">
            <ul class="nav nav-tabs nav-justified" role="tablist">
              <li role="presentation" class="active">
                <a href="#alipay" id="alipay-tab" role="tab" data-toggle="tab" aria-controls="alipay" aria-expanded="true"><i class="icon icon-alipay"></i> 支付宝</a>
              </li>
              <li role="presentation" class="">
                <a href="#wechatpay" role="tab" id="wechatpay-tab" data-toggle="tab" aria-controls="wechatpay" aria-expanded="false"><i class="icon icon-wepay"></i> 微信支付</a>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>



</main>

  <footer class="footer" itemscope itemtype="http://schema.org/WPFooter">
	
	
    <ul class="social-links">
    	
        <li><a href="https://gitee.com/ji-song" target="_blank" title="Github" data-toggle=tooltip data-placement=top><i class="icon icon-github"></i></a></li>
        
    </ul>

    <div class="copyright">
    	
        <div class="publishby">
        	Theme by <a href="https://github.com/cofess" target="_blank"> cofess </a>base on <a href="https://github.com/cofess/hexo-theme-pure" target="_blank">pure</a>.
        </div>
    </div>
</footer>
  <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')
</script>
<script src="/blog/js/plugin.min.js"></script>
<script src="/blog/js/application.js"></script>
  
    
    
    
        <script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(未命名)',
        },
        ROOT_URL: '/blog/',
        CONTENT_URL: '/blog/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>
<script src="/blog/js/insight.js"></script>
    
    
    
        


    
    
        
    
    



</body>
</html>